package com.vic.kjlink.order.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.vic.kjlink.order.model.Start;
import com.vic.kjlink.order.service.StartService;

import pers.vic.boot.base.model.BaseSelectModel;
import pers.vic.boot.base.model.PageInfo;
import pers.vic.boot.base.model.Remind;

/**
 * @description:开启点餐表 控制层
 * @author Vic.xu
 * @date: 2021-08-30 09:40
 */
@Controller
@RequestMapping("/start")
public class StartController extends BaseOrderController {

    private static final String FORM = "start-form";

    private static final String LIST = "start-list";

    private static final String REDIRECT_LIST = "redirect:/start/";
    @Resource
    private StartService startService;

    @GetMapping(value = {"/open"})
    public String open(Model model) {
        Start entity = startService.getLastOpen();
        List<BaseSelectModel> selects = startService.initStartSelectDatas(entity.getId());
        model.addAttribute("entity", entity);
        model.addAttribute("selects", selects);
        return FORM;
    }

    /**
     * 历史开启点餐列表
     * 
     * @param lookup
     * @param model
     * @return
     */
    @RequestMapping(value = {"", "/"}, method = {RequestMethod.GET, RequestMethod.POST})
    public String list(Start lookup, Model model) {
        PageInfo<Start> page = startService.page(lookup);
        model.addAttribute("pager", page);
        return LIST;
    }

    /**
     * 结束点餐
     * 
     * @param id
     * @param attrs
     * @return
     */
    @PostMapping(value = "/finish/{id}")
    public String save(@PathVariable int id, RedirectAttributes attrs) {
        startService.updateStatus(id, false);
        attrs.addFlashAttribute(Remind.success().setMessage("成功结束点餐"));
        return REDIRECT_LIST;
    }

    @PostMapping(value = {"/save"})
    public String save(Start entity, int[] code, RedirectAttributes attributes) {
        if (entity.getEndTime() == null) {
            attributes.addFlashAttribute(Remind.warning().setMessage("结束时间不可为空"));
            return "redirect:/start/open/";
        }
        if (code == null) {
            attributes.addFlashAttribute(Remind.warning().setMessage("请选择至少一个套餐"));
            return "redirect:/start/open/";
        }

        startService.save(entity, code);
        attributes.addFlashAttribute(Remind.success());
        return REDIRECT_LIST;

    }

}
